﻿<?php


class MessageMapper extends DataMapperAbstract
{
	public function __construct(DataAdapter $db, GenericArray $scripts)
	{
		parent::__construct($db);
		$this->_scripts = $scripts;
	}
	
	// list of related message
    public function find($id)
	{
		$this->query(sprintf($this->_scripts->message_list, $id));
		$messages = array();
		while($m = $this->fetch())
		{
			$messages[] = new Message($m);
		}
		return $messages;
	}
	
    // insert domain object (implemented by concrete domain object subclasses)
    public function save(GenericArray $message)
	{
		if(!isset($message->parent_id) || $message->parent_id <= 0)
			return $this->createTalk($message, $message->user);
		else
			return $this->replyTalk($message, $message->user);
	}
	
	public function listMessages($messageId, User $user)
	{
		if(!$this->isGuest($messageId, $user))
			return NULL;
		
		if($m = $this->getMessageParent($messageId))
		{
			//list all message related to the current parent
			$this->query(sprintf($this->_scripts->message_talk_list, $m->id, $m->id));
			$messages = array();
			while($m = $this->fetch())
			{
				$x = new Message($m);
				$messages[] = $x->getData();
			}
			return $messages;
		}
		return NULL;
	}
	
	//recupéré la liste des membres participant à un talk
	public function listGuests($messageId, User $user)
	{
		if(!$this->isGuest($messageId, $user))
			return NULL;
			
		if(($m = $this->getMessageParent($messageId)) !== null)
		{
			//list all message related to the current parent
			$query = sprintf($this->_scripts->message_users, $m->id);
			$this->query($query);
			echo "$query";
			$users = array();
			while($u = $this->fetch())
			{
				$u = new User($u);
				$users[] = $u->getData();
			}
			return $users;
		}
		return null;
	}
	
	public function addGuest($messageId, User $user)
	{
		if(($m = $this->getMessageParent($messageId)) !== null)
		{
			$this->query(sprintf($this->_scripts->guest_insert, $user->id, $m->id));
		}
	}
	
	public function removeGuest($messageId, User $user)
	{
		if(($m = $this->getMessageParent($messageId)) !== null)
		{
			$this->query(sprintf($this->_scripts->guest_delete, $user->id, $m->id));
		}
	}
	
	private function createTalk(GenericArray $message, User $user)
	{
		$this->query(sprintf($this->_scripts->message_new_talk, $message->title, $message->body, $user->id));
		$id = $this->getInsertID();
		$this->addGuest($id, $user, $user);
		return $id;
	}
	
	private function replyTalk(GenericArray $message, User $user)
	{
		if($this->isGuest($message->parent_id, $user))
		{
			if($m = $this->getMessageParent($message->parent_id))
			{
				$this->query(sprintf($this->_scripts->message_reply_talk, $m->id, $message->body, $user->id));
				return $this->getInsertID();
			}
		}
		return 0;
	}
	
	private function getMessageParent($messageId)
	{
		$query = sprintf($this->_scripts->message_talk_parent, $messageId, $messageId);
		$this->query($query);
		$m = new Message($this->fetch());
		return $m;
	}
	
	public function hasAuthority($messageId, $user)
	{
		if($user !== null && ($m = $this->getMessageParent($messageId)) !== null)
		{
			$this->query(sprintf($this->_scripts->message_auhtority,  $user->token, $m->id));
			return $this->countRows() > 0;
		}
		return FALSE;
	}
	
	private function isGuest($messageId, $user)
	{
		if($m = $this->getMessageParent($messageId))
		{
			$this->query(sprintf($this->_scripts->message_is_guest,  $user->email, $m->id));
			return $this->countRows() > 0;
		}
		return FALSE;
	}
	
	
    // update domain object (implemented by concrete domain object subclasses)
    public function update(GenericArray $message)
	{
		
	}
   
    // delete domain object (implemented by concrete domain object subclasses)
    public function delete(GenericArray $message)
	{
		
	}
}
